package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.VO.UserVO;
import com.example.demo.common.R;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/User")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录功能
     * @param user 实例化
     * @return 返回
     */
    @PostMapping("/login")
    public R<UserVO> login( @RequestBody User user){
        String password = user.getUserPassword();
        //System.out.print("用户名"+user.getUserId());
        password = DigestUtils.md5DigestAsHex(password.getBytes());// 密码md5加密
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,user.getUserId());//封装条件,查询用户
        User userRes = userService.getOne(queryWrapper);
        if(userRes == null){
            return R.error("登录失败");
        }
        if(!userRes.getUserPassword().equals(password)){
            return R.error("登录失败");
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userRes,userVO);
        String token = JWTUtils.generateToken(userRes.getUserId());
        userVO.setToken(token);
        return R.success(userVO);
    }

    /**
     *登出功能
     * @return 返回
     */
    @PostMapping("/logout")
    public R<String> logout(){
        return R.success("登出成功");
    }

    /**
     * 注册功能
     * @param user 用户类
     * @return 返回R
     */
    @PostMapping("/register")
    public R<String> saveNewUser(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,user.getUserId());
        if(userService.getOne(queryWrapper) != null){
            return R.error("用户ID已存在");
        }
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        user.setUserAdmin("不是");
        userService.save(user);
        return R.success("注册成功");
    }

    @PostMapping("/Webregister")
    public R<String> WebsaveNewUser(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,user.getUserId());
        if(userService.getOne(queryWrapper) != null){
            return R.error("用户ID已存在");
        }
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        user.setUserAdmin("不是");
        userService.save(user);
        return R.success("注册成功");
    }
    @PostMapping("/getAllButSelf")
    public R<List<String>> getAllUserIds(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId).notIn(User::getUserId,user.getUserId());
        List<User> ret = userService.list(queryWrapper);
        List<String> res = ret.stream().map(User::getUserId).collect(Collectors.toList());
        return R.success(res);
    }


    @GetMapping("/getList")
    public R<List<User>> getAllUser(){
        List<User> list = userService.list();
        return R.success(list);
    }

    @PostMapping("/getMyInfo")
    public R<User> getMyInfo(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        String UserId=user.getUserId();
        queryWrapper.eq(User::getUserId,UserId);
        User ans = userService.getOne(queryWrapper);

        return R.success(ans);
    }

    @PostMapping("/AddUser")
    public R<String> AddUser(@RequestBody User user){
        user.setUserPassword("e10adc3949ba59abbe56e057f20f883e");//默认设为123456的MD5格式
        //user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));//默认设为123456的MD5格式
        userService.save(user);
        return R.success("新建用户成功");
    }

    @PutMapping("/UpdateUser")
    public R<String> UpdateUser(@RequestBody User user){

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        //UpdateWrapper<MeetingRoom> updateWrapper = Wrappers.update();
        String UserId=user.getUserId();
        updateWrapper.eq("user_id",UserId);
        userService.update(user,updateWrapper );
        return R.success("修改用户成功");
    }

    @PutMapping("/UpdateUserPassword")
    public R<String> UpdateUserPassword(@RequestBody User user){
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        //UpdateWrapper<MeetingRoom> updateWrapper = Wrappers.update();
        String UserId=user.getUserId();
        updateWrapper.eq("user_id",UserId);
        userService.update(user,updateWrapper );
        return R.success("修改密码成功");

    }


    @PutMapping("/DeleteUser")
    public R<String> DeleteUser(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String UserId=user.getUserId();
        wrapper.eq("user_id",UserId);
        userService.remove(wrapper);
        return R.success("删除用户成功");

    }


}
